Source for file SC_Query.php
Documentation is available at SC_Query.php
* This file is part of EC-CUBE
* Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
* http://www.lockon.co.jp/
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* @author LOCKON CO.,LTD.
* @version $Id: SC_Query.php 18735 2010-06-22 08:53:31Z nanasess $
* @param boolean $err_disp エラー表示を行うかどうか
* @param boolean $new 新規に接続を行うかどうか
function SC_Query($dsn = "", $err_disp = true, $new = false) {
$this->conn = new SC_DBconn($dsn, $err_disp, $new);
if(PEAR::isError($this->conn->conn)) {
* @param string $table テーブル名
* @param string $where where句
* @param array $arrval プレースホルダ
function count($table, $where = "", $arrval = array()) {
$sqlse = "SELECT COUNT(*) FROM $table";
$sqlse = "SELECT COUNT(*) FROM $table WHERE $where";
$ret = $this->conn->getOne($sqlse, $arrval);
* @param string $col カラム名. 複数カラムの場合はカンマ区切りで書く
* @param string $table テーブル名
* @param string $where WHERE句
* @param array $arrval プレースホルダ
function select($col, $table, $where = "", $arrval = array()){
$sqlse = $this->getSql($col, $table, $where);
$dbFactory = SC_DB_DBFactory_Ex::getInstance();
$sqlse = $dbFactory->sfChangeMySQL($sqlse);
$ret = $this->conn->getAll($sqlse, $arrval);
* @param boolean $disp trueの場合、画面出力を行う.
$sql = $this->conn->conn->last_query;
$this->conn->query("COMMIT");
$this->conn->query("BEGIN");
$this->conn->query("ROLLBACK");
function exec($str, $arrval = array()) {
$this->conn->query($str, $arrval);
function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) {
foreach ($arrwhere as $key => $val) {
$strw .= $key . " LIKE ?";
} else if(strlen($arrcon[$key]) > 0) {
$strw .= " ". $arrcon[$key]. " " . $key . " LIKE ?";
$strw .= " AND " . $key . " LIKE ?";
$sqlse = "SELECT $col FROM $table WHERE $strw ". $this->option;
$sqlse = "SELECT $col FROM $table ". $this->option;
$ret = $this->conn->getAll($sqlse, $arrval);
function getAll($sql, $arrval = array()) {
$ret = $this->conn->getAll($sql, $arrval);
function getSql($col, $table, $where) {
$sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
$sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
$sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
$option = " LIMIT " . $limit;
$option.= " OFFSET " . $offset;
$this->groupby = "GROUP BY " . $str;
$this->where .= " AND " . $str;
$this->where .= " OR " . $str;
$this->order = "ORDER BY " . $str;
$this->option = " LIMIT " . $limit;
$this->offset = " OFFSET " . $offset;
* @param string $table テーブル名
* @param array $sqlval array('カラム名' => '値',...)の連想配列
function insert($table, $sqlval) {
if(count($sqlval) <= 0 ) return false;
foreach ($sqlval as $key => $val) {
if(eregi("^Now\(\)$", $val)) {
$sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
$ret = $this->conn->query($sqlin, $arrval);
// $sqlval :列名 => 値の格納されたハッシュ配列
foreach ($sqlval as $key => $val) {
$eval = pg_escape_string($val);
$sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
$ret = $this->conn->query($sqlin);
* @param string $table テーブル名
* @param array $sqlval array('カラム名' => '値',...)の連想配列
* @param string $where WHERE句
* @param array $arradd $addcol用のプレースホルダ配列
* @param string $addcol 追加カラム
function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
foreach ($sqlval as $key => $val) {
if(eregi("^Now\(\)$", $val)) {
$strcol .= $key . '= Now(),';
$strcol .= $key . '= ?,';
foreach($addcol as $key => $val) {
$strcol .= "$key = $val,";
$sqlup = "UPDATE $table SET $strcol WHERE $where";
$sqlup = "UPDATE $table SET $strcol";
foreach($arradd as $val) {
$ret = $this->conn->query($sqlup, $arrval);
function max($table, $col, $where = "", $arrval = array()) {
$sqlse = "SELECT MAX($col) FROM $table";
$sqlse = "SELECT MAX($col) FROM $table WHERE $where";
$ret = $this->conn->getOne($sqlse, $arrval);
function min($table, $col, $where = "", $arrval = array()) {
$sqlse = "SELECT MIN($col) FROM $table";
$sqlse = "SELECT MIN($col) FROM $table WHERE $where";
$ret = $this->conn->getOne($sqlse, $arrval);
function get($table, $col, $where = "", $arrval = array()) {
$sqlse = "SELECT $col FROM $table";
$sqlse = "SELECT $col FROM $table WHERE $where";
$ret = $this->conn->getOne($sqlse, $arrval);
function getOne($sql, $arrval = array()) {
$ret = $this->conn->getOne($sql, $arrval);
function getRow($table, $col, $where = "", $arrval = array()) {
$sqlse = "SELECT $col FROM $table";
$sqlse = "SELECT $col FROM $table WHERE $where";
$ret = $this->conn->getRow($sqlse, $arrval);
function getCol($table, $col, $where = "", $arrval = array()) {
$sqlse = "SELECT $col FROM $table";
$sqlse = "SELECT $col FROM $table WHERE $where";
return $this->conn->getCol($sqlse, 0, $arrval);
* @param string $table テーブル名
* @param string $where WHERE句
* @param array $arrval プレースホルダ
function delete($table, $where = "", $arrval = array()) {
$sqlde = "DELETE FROM $table";
$sqlde = "DELETE FROM $table WHERE $where";
$ret = $this->conn->query($sqlde, $arrval);
function nextval($table, $colname) {
// postgresqlとmysqlとで処理を分ける
if (DB_TYPE == "pgsql") {
$seqtable = $table . "_" . $colname . "_seq";
$sql = "SELECT NEXTVAL('$seqtable')";
}else if (DB_TYPE == "mysql") {
$sql = "SELECT last_insert_id();";
$ret = $this->conn->getOne($sql);
function currval($table, $colname) {
if (DB_TYPE == "pgsql") {
$seqtable = $table . "_" . $colname . "_seq";
$sql = "SELECT CURRVAL('$seqtable')";
}else if (DB_TYPE == "mysql") {
$sql = "SELECT last_insert_id();";
$ret = $this->conn->getOne($sql);
function setval($table, $colname, $data) {
if (DB_TYPE == "pgsql") {
$seqtable = $table . "_" . $colname . "_seq";
$sql = "SELECT SETVAL('$seqtable', $data)";
$ret = $this->conn->getOne($sql);
}else if (DB_TYPE == "mysql") {
$sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
$ret = $this->conn->query($sql);
function query($n ,$arr = "", $ignore_err = false){
$result = $this->conn->query($n, $arr, $ignore_err);
* @param string $table_name テーブル名
$this->query("LOCK TABLES $table_name WRITE");
$arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
$auto_inc_no = $arrRet[0]["Auto_increment"];
$this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
$this->query('UNLOCK TABLES');
Documentation generated on Fri, 24 Feb 2012 14:00:20 +0900 by Seasoft
|